package com.wang.controller;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestAttribute;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import sun.security.pkcs11.Secmod;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.Map;

@Controller
public class RequestAttributeController {
    @GetMapping("/r1")
    public String r1(HttpServletRequest request){
        request.setAttribute("a", "aa");
        request.setAttribute("b", 18);
        return "forward:/r2";
    }
    /*
    * @RequestAttribute("a")
    * 该注解可以将request中的值直接赋值给方法中的参数*/
    @ResponseBody
    @GetMapping("/r2")
    public Map<String, Object> r2(HttpServletRequest request,
                                  @RequestAttribute("a") String a1,
                                  @RequestAttribute("b") int b1){
        HashMap<String, Object> map = new HashMap<>();
        Object a = request.getAttribute("a");
        Object b = request.getAttribute("b");

        map.put("a", a);
        map.put("b", b);
        map.put("a1", a1);
        map.put("b1", b1);

        System.out.println(a);
        System.out.println(b);
        System.out.println(a1);
        System.out.println(b1);

        return map;
    }
    @GetMapping("/r3")
    public String r3(HttpServletRequest request, Map<String, Object> map, Model model){
        request.setAttribute("a", "aa");
        map.put("b", "bbb");
        model.addAttribute("c", "ccc");
        return "forward:/r4";
    }
    @GetMapping("/r4")
    @ResponseBody
    public Map<String, Object> r4(HttpServletRequest request){
        Object a = request.getAttribute("a");
        Object b = request.getAttribute("b");
        Object c = request.getAttribute("c");
        HashMap<String, Object> map = new HashMap<>();
        map.put("a", a);
        map.put("b", b);
        map.put("c", c);

        System.out.println(map);
        return map;
    }
}
